﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

using H2.NET.Controls;
using H2.NET.Data.Common;
using H2.NET.Business;

namespace H2.NET.Web.Pages
{
    public partial class TabTJO : TaskControl
    {
        #region 属性 Public
        public MDCTabStatus Status
        {
            get
            {
                if (StoreAPI["Status~" + Key] == null)
                {
                    StoreAPI["Status~" + Key] = new MDCTabStatus();
                }

                return (MDCTabStatus)StoreAPI["Status~" + Key];
            }
            set
            {
                Key = value.KeyWord;
                StoreAPI["Status~" + Key] = value;
            }
        }

        public string Key
        {
            get
            {
                return ViewState["Key"] == null ? "" : (string)ViewState["Key"];

            }
            set { ViewState["Key"] = value; }
        }

        public int SheetIndex
        {
            get
            {
                return ViewState["SheetIndex"] == null ? 0 : (int)ViewState["SheetIndex"];

            }
            set { ViewState["SheetIndex"] = value; }
        }

        public bool IsLoaded
        {
            get
            {
                return ViewState["IsLoaded"] == null ? false : (bool)ViewState["IsLoaded"];
            }
            set { ViewState["IsLoaded"] = value; }
        }

        public string TJO_ProductionLine
        {
            get
            {
                return ViewState["TJO_ProductionLine"] == null ? "" : (string)ViewState["TJO_ProductionLine"];

            }
            set { ViewState["TJO_ProductionLine"] = value; }
        }

        public string TJO_Color
        {
            get
            {
                return ViewState["TJO_Color"] == null ? "" : (string)ViewState["TJO_Color"];

            }
            set { ViewState["TJO_Color"] = value; }
        }
        #endregion

        #region 事件 Load (页面)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Status.IsUsed)
            {
                return;
            }

            bool flg = TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit;
            bool flg1 = (Status.Technical == "C" || Status.Technical == "Sample is received") && TaskAPI.IsAllowSubmit;
            gv_TJO_1.DynamicColumn.Add(new BoundColumn2("00", "客户编号", BoundColumnType2.TextBox, 200, Align.Left, flg, "CustomerCode", InputType.None, null));
            gv_TJO_1.DynamicColumn.Add(new BoundColumn2("01", "客户名称", BoundColumnType2.TextBox, 300, Align.Left, flg, "CustomerName", InputType.None, null));
            gv_TJO_1.DynamicColumn.Add(new BoundColumn2("02", "产品线", BoundColumnType2.TextBox, 200, Align.Left, flg, "ProductionLine", InputType.None, null));
            gv_TJO_1.DynamicColumn.Add(new BoundColumn2("03", "颜色", BoundColumnType2.TextBox, 150, Align.Left, flg, "Color", InputType.None, null));
            gv_TJO_1.DynamicColumn.Add(new BoundColumn2("04", "包装", BoundColumnType2.TextBox, 100, Align.Left, flg, "Package", InputType.None, null));
            gv_TJO_1.DynamicColumn.Add(new BoundColumn2("05", "数量", BoundColumnType2.TextBox, 100, Align.Left, flg, "Amount", InputType.None, null));
            gv_TJO_1.DynamicColumn.Add(new BoundColumn2("06", "销售及快递信息", BoundColumnType2.TextBox, 100, Align.Left, flg, "ICINo", InputType.None, null));
            //gv_TJO_1.DynamicColumn.Add(new BoundColumn2("06", "销售信息", BoundColumnType2.TextBox, 100, Align.Left, flg, "ICINo", InputType.None, null));
            gv_TJO_1.DynamicColumn.Add(new BoundColumn2("07", "电脑单号", BoundColumnType2.TextBox, 100, Align.Left, flg, "PCNo", InputType.None, null));
            gv_TJO_1.DynamicColumn.Add(new BoundColumn2("08", "特编色号", BoundColumnType2.TextBox, 100, Align.Left, flg1, "BatchNo", InputType.None, null));
            gv_TJO_1.DynamicColumn.Add(new BoundColumn2("09", "调色基漆号", BoundColumnType2.TextBox, 100, Align.Left, flg1, "Barcode", InputType.None, null));
            gv_TJO_1.DynamicColumn.Add(new BoundColumn2("10", "备注", BoundColumnType2.TextBox, 100, Align.Left, flg, "CustomerType", InputType.None, null));
            //gv_TJO_1.DynamicColumn.Add(new BoundColumn2("10", "订单状态", BoundColumnType2.DropDownList, 300, Align.Left, flg, "OrderStatus", InputType.None, new string[12] { "", "订单收到，处理中", "8－已出货", "6－部分出货", "F-已打单", "客户取消订单", "其他订单审批（特价，送货地址）", "产品缺货", "订单未收到，需要客户重传订单", "PH - 价格待审批", "H - 信贷待审批", "H+PH - 价格/信贷待审批" }));
            //gv_TJO_1.DynamicColumn.Add(new BoundColumn2("11", "配套体系", BoundColumnType2.DropDownList, 100, Align.Left, flg, "SupportSystem", InputType.None, new string[5] { "", "A4纸板", "高密度板", "铁板", "特殊样板" }));

            //gv_TJO_1.DynamicColumn.Add(new BoundColumn2("13", "样本类型", BoundColumnType2.TextBox, 100, Align.Left, flg, "SampleType", InputType.None, null));
            //gv_TJO_1.DynamicColumn.Add(new BoundColumn2("14", "样品数量", BoundColumnType2.TextBox, 100, Align.Left, flg, "SampleNumber", InputType.None, null));
            gv_TJO_1.Width = 1000;
            SetEnable(flg, TJO_1_Add);
            SetEnable(flg, TJO_1_Delete);
            if (!Page.IsPostBack)
            {
                InitControls();
            }
        }
        #endregion

        #region 事件 Add(增加行)
        protected void Add_Click(object sender, EventArgs e)
        {
            switch (((Anthem.Button)sender).ID)
            {
                case "TJO_1_Add":
                    List<mdc_TJO> tjo_1_s = (List<mdc_TJO>)gv_TJO_1.DataSource;
                    mdc_TJO tjo_1 = new mdc_TJO();
                    tjo_1.No = tjo_1_s.Count + 1;
                    tjo_1_s.Insert(gv_TJO_1.ChangedRowIndex == -1 || gv_TJO_1.ChangedRowIndex >= tjo_1_s.Count ? tjo_1_s.Count - 1 : gv_TJO_1.ChangedRowIndex, tjo_1);
                    gv_TJO_1.DataSource = tjo_1_s;
                    gv_TJO_1.DataBind();
                    break;
            }
        }
        #endregion

        #region 事件 Delete(删除行)
        protected void Delete_Click(object sender, EventArgs e)
        {
            switch (((Anthem.Button)sender).ID)
            {
                case "TJO_1_Delete":
                    List<mdc_TJO> tjo_1_s = (List<mdc_TJO>)gv_TJO_1.DataSource;
                    IList<mdc_TJO> list = (mdc_TJO.process == TaskAPI.Process & mdc_TJO.incident == TaskAPI.Incident).List<mdc_TJO>();
                    if (tjo_1_s.Count == 1)
                    {
                        Anthem.Manager.AddScriptForClientSideEval("JSInfomation('至少保留最后一个 Sheet ！',false)");
                        return;
                    }
                    tjo_1_s.RemoveAt(gv_TJO_1.ChangedRowIndex == -1 || gv_TJO_1.ChangedRowIndex >= tjo_1_s.Count ? tjo_1_s.Count - 1 : gv_TJO_1.ChangedRowIndex);
                    bool flag = false;
                    foreach (mdc_TJO tjo_1 in list)
                    {
                        if (string.IsNullOrEmpty(tjo_1.ProductionLine))
                        {
                            continue;
                        }
                        foreach (mdc_TJO tjo_2 in tjo_1_s)
                        {

                            if (string.IsNullOrEmpty(tjo_1.ProductionLine))
                            {
                                continue;
                            }
                            if (tjo_2.ID == tjo_1.ID)
                            {
                                flag = true;
                                break;
                            }
                        }
                        if (!flag)
                        {
                            Storage.Delete(tjo_1);
                        }
                        flag = false;
                    }

                    gv_TJO_1.DataSource = tjo_1_s;
                    gv_TJO_1.DataBind();
                    break;
            }
        }
        #endregion

        #region 方法 Save(保存数据库)
        public void Save()
        {
            if (!Status.IsEdited)
            {
                return;
            }
            List<string> cmd_tar = new List<string>();
            List<string> cmd_tar1 = new List<string>();
            int i = 1;
            foreach (mdc_TJO tjo in (List<mdc_TJO>)gv_TJO_1.DataSource)
            {
                //if (string.IsNullOrEmpty(tjo.ProductionLine))
                //{
                //    continue;
                //}

                tjo.Process = TaskAPI.Process;
                tjo.Incident = TaskAPI.Incident;
                tjo.UserName = UserAPI.UserName;
                tjo.Time = System.DateTime.Now;
                tjo.Status = 1;
                cmd_tar.Add(tjo.ProductionLine);
                cmd_tar1.Add(tjo.Color);
                Storage.Save(tjo);
            }
            TaskAPI["TJO_ProductionLine"] = cmd_tar.Count == 0 ? new string[1] { "" } : cmd_tar.ToArray();
            TaskAPI["TJO_Color"] = cmd_tar1.Count == 0 ? new string[1] { "" } : cmd_tar1.ToArray();
        }
        #endregion

        #region 方法 InitControls(初始化)
        public void InitControls()
        {
            gv_TJO_1.Key = Key;
            if (TaskAPI.IsInitiate)
            {
                List<mdc_TJO> tjo_s = new List<mdc_TJO>();
                for (int i = 0; i < 10; i++)
                {
                    tjo_s.Add(new mdc_TJO());
                    tjo_s[i].No = i + 1;
                }
                gv_TJO_1.DataSource = tjo_s;
            }
            else
            {
                IList<mdc_TJO> tjo_s = (mdc_TJO.process == TaskAPI.Process & mdc_TJO.incident == TaskAPI.Incident & mdc_TJO.status == 1).List<mdc_TJO>();
                if (tjo_s.Count == 0)
                {
                    for (int i = 0; i < 10; i++)
                    {
                        tjo_s.Add(new mdc_TJO());
                        tjo_s[i].No = i + 1;
                    }
                }
                gv_TJO_1.DataSource = tjo_s;
            }
        }
        #endregion

        #region 方法 gv_TJO_1 数据取得
        protected void gv_TJO_1_DataBinding(object sender, EventArgs e)
        {
            //if (TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit && gv_CCC_1.ChangedColumnIndex == 9)
            if (TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit && (gv_TJO_1.ChangedColumnIndex == 0 || gv_TJO_1.ChangedColumnIndex == 1))
            {
                if (TaskAPI.Process == "TJO Base Code Application")
                {
                    List<mdc_TJO> tjo_s = gv_TJO_1.DataSource as List<mdc_TJO>;

                    foreach (mdc_TJO tjo in tjo_s)
                    {
                        m_Customer ncc = (m_Customer.customerCode == tjo.CustomerCode).ListFirst<m_Customer>();
                        if (ncc != null)
                        {
                            tjo.CustomerName = ncc.CustomerName;
                        }
                        else
                        {
                            tjo.CustomerName = "";
                        }
                    }
                }
                else if (TaskAPI.Process == "HK-TJO Base Code Application")
                {

                }

            }
        }
        #endregion

        #region 方法 DataValidation(数据验证)
        public List<string> DataValidation()
        {
            //如果当前 Tabs 是隐藏的情况，退出
            if (!(Status.IsUsed && (TaskAPI.IsBeginStep || TaskAPI.Step == "Technical" || TaskAPI.Step == "Sample is received") && TaskAPI.IsAllowSubmit))
            {
                return new List<string>();
            }

            List<string> vald_s = new List<string>();
            List<Regular> regs = new List<Regular>();
            foreach (mdc_TJO tjo in (List<mdc_TJO>)gv_TJO_1.DataSource)
            {
                if (TaskAPI.Process == "TJO Base Code Application")
                {
                    if (!string.IsNullOrEmpty(tjo.ProductionLine))
                    {
                        regs.Add(new Regular(RegularExp.NotEmpty, tjo.ProductionLine, "&nbsp;&nbsp;产品线为必填"));
                        regs.Add(new Regular(RegularExp.NotEmpty, tjo.Color, "&nbsp;&nbsp;颜色为必填"));
                        regs.Add(new Regular(RegularExp.NotEmpty, tjo.Package, "&nbsp;&nbsp;包装为必填"));
                        regs.Add(new Regular(RegularExp.NotEmpty, tjo.CustomerCode, "&nbsp;&nbsp;客户编号为必填"));
                        regs.Add(new Regular(RegularExp.NotEmpty, tjo.CustomerName, "&nbsp;&nbsp;客户编号不存在"));
                        //王航2017.5.31号新增

                        regs.Add(new Regular(RegularExp.NotEmpty, tjo.ICINo, "&nbsp;&nbsp;销售及快递信息为必填(常规TJO填0即可)"));
                        /*if (string.IsNullOrEmpty(tjo.ICINo) && TaskAPI.IsBeginStep && !TaskAPI.IsAllowSubmit)
                        {
                            regs.Add(new Regular(RegularExp.NotEmpty, tjo.ICINo, "&nbsp;&nbsp;销售及快递信息为必填"));
                        }*/
                        if (TaskAPI.Step == "Technical" || TaskAPI.Step == "Sample is received")
                        {
                            regs.Add(new Regular(RegularExp.NotEmpty, tjo.BatchNo, "&nbsp;&nbsp;特编色号为必填"));
                            regs.Add(new Regular(RegularExp.NotEmpty, tjo.Barcode, "&nbsp;&nbsp;调色基漆号为必填"));
                        }
                    }
                }
                else if (TaskAPI.Process == "HK-TJO Base Code Application")
                {
                    if (string.IsNullOrEmpty(tjo.ProductionLine) && string.IsNullOrEmpty(tjo.Color) && string.IsNullOrEmpty(tjo.Package) && string.IsNullOrEmpty(tjo.CustomerCode) && string.IsNullOrEmpty(tjo.CustomerName))
                    {
                        continue;
                    }
                    regs.Add(new Regular(RegularExp.NotEmpty, tjo.ProductionLine, "&nbsp;&nbsp;产品线为必填"));
                }
            }

            vald_s.AddRange(Validation(regs));

            if (vald_s.Count > 0)
            {
                vald_s.Insert(0, "<font color='#0000FF'><b>" + Status.Name + "</b></font>");
                vald_s.Add("&nbsp;&nbsp;-----------------------------------------");
            }

            return vald_s;



        }
        #endregion
    }
}
